<template>
  <ContentWrap title="搜索" />

  <ContentWrap title="列表" class="space-y-20px">
    <el-table v-loading="tableLoading" :data="tableData" border class="space-top" stripe>
      <el-table-column label="序号" prop="id" />
      <el-table-column label="作品名称" prop="title" />
      <el-table-column label="用户" prop="name" />
      <el-table-column label="图片">
        <template #default="scope">
          <el-image
            style="width: 30px; height: 30px"
            :src="scope.row.img_url"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[scope.row.img_url]"
            :initial-index="4"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.state === 1" type="primary">上架中</el-tag>
          <el-tag v-else-if="scope.row.state === 2" type="success">下架中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="作品日期" prop="created_at" />
      <el-table-column label="操作">
        <template #default="scope">
          <div class="action-btns">
            <div @click="openDetail(scope.row)">查看</div>
            <div class="pass-btn" v-if="scope.row.state !== 1" @click="updateTopState(scope.row, 1)"
              >上架
            </div>
            <div
              class="reject-btn"
              v-if="scope.row.state !== 2"
              @click="updateTopState(scope.row, 2)"
              >下架
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="SearchDict.page"
      v-model:page-size="SearchDict.page_size"
      :total="total"
      background
      class="mt-4 flex-center"
      layout="total, prev, pager, next, sizes, jumper"
      small
      @current-change="pageChange"
      @size-change="sizeChange"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { onMounted, ref } from 'vue'
import request from '@/utils/request'
import { ElNotification } from 'element-plus'

const SearchDict = ref({
  name: '',
  title: '',
  page: 1,
  page_size: 10,
  state: ''
})

const tableLoading = ref(false)
const tableData = ref([])
const total = ref(0)

const resetSearch = () => {
  SearchDict.value = {
    name: '',
    title: '',
    page: 1,
    page_size: 10,
    state: ''
  }
  getTableData()
}

const getTableData = () => {
  request({
    url: 'all/top',
    method: 'post',
    data: {
      page: SearchDict.value.page,
      page_size: SearchDict.value.page_size,
      name: SearchDict.value.name,
      title: SearchDict.value.title,
      state: parseInt(SearchDict.value.state)
    }
  }).then((res) => {
    if (res.code === 200) {
      tableData.value = res.data
      total.value = res.count
    }
  })
}

const pageChange = (page) => {
  SearchDict.value.page = page
  getTableData()
}

const sizeChange = (size) => {
  SearchDict.value.page_size = size
  getTableData()
}

const updateTopState = (item, state) => {
  request({
    url: 'top/update',
    method: 'post',
    data: {
      id: item.id,
      state: state
    }
  }).then((res) => {
    if (res.code === 200) {
      ElNotification.success('操作成功!')
    } else {
      ElNotification.error(res.msg)
    }
  })
}

onMounted(() => {
  getTableData()
})
</script>

<style scoped lang="less">
.action-btns {
  display: flex;
  flex-direction: row;

  div {
    margin-right: 10px;
    cursor: pointer;
  }

  div:hover {
    opacity: 0.7;
  }

  div:first-child {
    color: var(--el-color-primary);
  }

  .pass-btn {
    color: var(--el-color-success);
  }

  .reject-btn {
    color: var(--el-color-danger);
  }
}
</style>
